# -*- coding: utf-8 -*-

import json
from urllib.request import urlopen
from urllib.parse import quote

# 经纬度查询
def search_lnglat(addr):
    '''默认都能取到数据，未做异常处理'''
    # 高德地图服务
    url = "https://restapi.amap.com/v3/geocode/geo?"
    # 参数
    key = "c4b9bb6b34cc25ce296925d0f557f75c"
    address = quote(addr)
    # 请求URI
    uri = url + "address={}&key={}".format(address, key)
    req = urlopen(uri)
    # 解析
    res = req.read()
    res = res.decode()
    res_dict = json.loads(res)
    # 取数
    if res_dict["status"] == 0:
        return 0, 0
    location = res_dict["geocodes"][0]["location"]
    # 切分经纬度
    lng, lat = location.split(",")
    return float(lng), float(lat)